﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPS.Common;
using System.Linq.Dynamic;

namespace EPS.DataAccess
{
    public class CommonService
    {
        public void InsertEntity<T>(T entity)
            where T : class
        {

            using (CommonDataContext dc = new CommonDataContext())
            {
                dc.GetTable<T>().InsertOnSubmit(entity);
                dc.SubmitChanges();
            }
        }

        public void InsertEntity<T>(IEnumerable<T> list) where T : class
        {
            using (CommonDataContext dc = new CommonDataContext())
            {
                dc.GetTable<T>().InsertAllOnSubmit(list);
                dc.SubmitChanges();
            }
        }

        public void UpdateEntity<T>(T entity)
            where T : class
        {
            using (CommonDataContext dc = new CommonDataContext())
            {
                dc.GetTable<T>().Attach(entity.DeepCopy(), true);
                dc.SubmitChanges();
            }
        }

        public void UpdateEntity<T>(IEnumerable<T> list) where T : class
        {
            using (CommonDataContext dc = new CommonDataContext())
            {
                dc.GetTable<T>().AttachAll(list.DeepCopy(), true);
                dc.SubmitChanges();
            }
        }

        public void DeleteEntity<T>(T entity) where T : class
        {
            using (CommonDataContext dc = new CommonDataContext())
            {
                dc.GetTable<T>().DeleteOnSubmit(entity);
                dc.SubmitChanges();
            }
        }

        public void DeleteEntity<T>(System.Linq.Expressions.Expression<Func<T, bool>> predicate) where T : class
        {
            using (CommonDataContext dc = new CommonDataContext())
            {
                var query = dc.GetTable<T>().Where(predicate);
                dc.GetTable<T>().DeleteAllOnSubmit(query);
                dc.SubmitChanges();
            }
        }

        public bool Check<T>(System.Linq.Expressions.Expression<Func<T, bool>> predicate) where T : class
        {
            using (CommonDataContext dc = new CommonDataContext())
            {
                return dc.GetTable<T>().Where(predicate).Any();
            }
        }

        public T GetEntity<T>(System.Linq.Expressions.Expression<Func<T, bool>> predicate) where T : class
        {
            using (CommonDataContext dc = new CommonDataContext())
            {
                return dc.GetTable<T>().SingleOrDefault(predicate);

            }
        }

        public List<T> GetByLamda<T>(System.Linq.Expressions.Expression<Func<T, bool>> predicate) where T : class
        {
            using (CommonDataContext dc = new CommonDataContext())
            {
                return dc.GetTable<T>().Where(predicate).ToList();
            }
        }

        public List<T> GetBySql<T>(string sql) where T : class
        {
            using (CommonDataContext dc = new CommonDataContext())
            {
                return dc.GetTable<T>().Where(sql).ToList();
            }
        }

        public SearchResult<T> GetByPage<T>(SearchFilter filter) where T : class
        {
            using (CommonDataContext dc = new CommonDataContext())
            {
                SearchResult<T> result = new SearchResult<T>();
                if (filter != null)
                {
                    if(filter.PageSize==0) return result;
                    var query = dc.GetTable<T>().Where(filter.Filter);
                    result.RecordCount = query.Count();
                    if (!string.IsNullOrEmpty(filter.OrderBy))
                        query = query.OrderBy(filter.OrderBy);
                    if (result.RecordCount == 0) return result;
                    query.Skip(filter.CurrentPage * filter.PageSize).Take(filter.PageSize);
                    result.Result = query.ToArray();
                    result.PageCount = result.RecordCount / filter.PageSize;
                }
                return result;
            }
        }


    }
}
